package com.cibercol.client;


import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.Slider;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.CheckBoxGroup;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.SliderField;
import com.extjs.gxt.ui.client.widget.form.SpinnerField;
import com.extjs.gxt.ui.client.widget.form.TextArea;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FlowLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.VerticalPanel;


public class Formulario extends LayoutContainer {

	private VerticalPanel vp;
	private FormData data;
	
	/**
	 * al cargar la pagina inicializa los elementos
	 */
	@Override
	protected void onRender(Element parent, int index) {
		// TODO Auto-generated method stub
		super.onRender(parent, index);
		data=new FormData("-20");
		vp=new VerticalPanel();
		vp.setSpacing(10);
		createFormSimple();
		createFormField();
		add(vp);
	}
	/**
	 * Metodo que crea el formulario sencillo
	 */
	private void createFormSimple(){
		FormPanel simple=new FormPanel();
		simple.setHeading("Formulario simple");
		simple.setFrame(true);
		simple.setWidth(350);
		
		TextField<String> nombre=new TextField<String>();
		nombre.setFieldLabel("Nombre: ");
		nombre.setAllowBlank(false);
		nombre.getFocusSupport().setPreviousId(simple.getButtonBar().getId());
		simple.add(nombre,data);
		
		TextField<String> email=new TextField<String>();
		email.setFieldLabel("Email");
		email.setAllowBlank(false);
		simple.add(email,data);

		List<Stock>stocks=Stock.getStocks();
		Collections.sort(stocks,new Comparator<Stock>() {
			public int compare(Stock st, Stock st1){
				return st.getStr1().compareTo(st1.getStr1());
			}
		});
		
		ListStore<Stock>store=new ListStore<Stock>();
		store.add(stocks);
		
		ComboBox<Stock> box=new ComboBox<Stock>();
		box.setFieldLabel("Company");
		box.setDisplayField("name");
		box.setTriggerAction(TriggerAction.ALL);
		box.setStore(store);
		simple.add(box,data);
		
		DateField field=new DateField();
		field.setFieldLabel("Time");
		simple.add(field,data);
		
		Slider slider=new Slider();
		slider.setMinValue(40);
		slider.setMaxValue(90);
		slider.setValue(60);
		slider.setIncrement(1);
		slider.setMessage("{0} inches all");
		
		final SliderField sf=new SliderField(slider);
		sf.setFieldLabel("Size");
		simple.add(sf,data);
		
		CheckBox check1 = new CheckBox();  
	    check1.setBoxLabel("Classical");  
	  
	    CheckBox check2 = new CheckBox();  
	    check2.setBoxLabel("Rock");  
	    check2.setValue(true);  
	  
	    CheckBox check3 = new CheckBox();  
	    check3.setBoxLabel("Blue"); 
	    
	    CheckBoxGroup group=new CheckBoxGroup();
	    group.setFieldLabel("Music");
	    group.add(check1);
	    group.add(check2);
	    group.add(check3);
	    simple.add(group,data);
	    
	    Radio radio = new Radio();  
	    radio.setBoxLabel("Red");  
	    radio.setValue(true);  
	  
	    Radio radio2 = new Radio();  
	    radio2.setBoxLabel("Blue");  
	  
	    RadioGroup radioGroup = new RadioGroup();  
	    radioGroup.setFieldLabel("Favorite Color");  
	    radioGroup.add(radio);  
	    radioGroup.add(radio2);  
	    simple.add(radioGroup, data);  
	  
	    Radio radio3 = new Radio();  
	    radio3.setBoxLabel("Apple");  
	    radio3.setValue(true);  
	  
	    Radio radio4 = new Radio();  
	    radio4.setBoxLabel("Banana");  
	  
	    RadioGroup radioGroup2 = new RadioGroup();  
	    radioGroup2.setFieldLabel("Favorite Fruit");  
	    radioGroup2.add(radio3);  
	    radioGroup2.add(radio4);  
	    simple.add(radioGroup2, data);  
	    TextArea description = new TextArea();  
	    description.setPreventScrollbars(true);  
	    description.setFieldLabel("Description");  
	    simple.add(description, data);  
	  
	    Button b = new Button("Submit");  
	    simple.addButton(b);  
	    simple.addButton(new Button("Cancel"));  
	    
	    simple.setButtonAlign(HorizontalAlignment.CENTER);  
	    
	    FormButtonBinding binding = new FormButtonBinding(simple);  
	    binding.addButton(b);  
	  
	    SpinnerField spinnerField = new SpinnerField();  
	    spinnerField.setIncrement(.1d);  
	    spinnerField.getPropertyEditor().setType(Double.class);  
	    spinnerField.getPropertyEditor().setFormat(NumberFormat.getFormat("00.0"));  
	    spinnerField.setFieldLabel("Duration (s)");  
	    spinnerField.setMinValue(-10d);  
	    spinnerField.setMaxValue(10d);  
	    simple.add(spinnerField, data);  
	  
	    vp.add(simple); 
		
	}
	/**
	 * Metodo que crea el formulario con fieldset
	 */
	private void createFormField(){
		FormPanel form2 = new FormPanel();  
	    form2.setFrame(true);  
	    form2.setHeading("Simple Form with FieldSets");  
	    form2.setWidth(350);  
	    form2.setLayout(new FlowLayout());  
	  
	    FieldSet fieldSet = new FieldSet();  
	    fieldSet.setHeading("User Information");  
	    fieldSet.setCheckboxToggle(true);  
	  
	    FormLayout layout = new FormLayout();  
	    layout.setLabelWidth(75);  
	    fieldSet.setLayout(layout);  
	  
	    TextField<String> firstName = new TextField<String>();  
	    firstName.setFieldLabel("First Name");  
	    firstName.setAllowBlank(false);  
	    fieldSet.add(firstName, data);  
	  
	    TextField<String> lastName = new TextField<String>();  
	    lastName.setFieldLabel("Last Name");  
	    fieldSet.add(lastName, data);  
	  
	    TextField<String> company = new TextField<String>();  
	    company.setFieldLabel("Company");  
	    fieldSet.add(company, data);  
	  
	    TextField<String> email = new TextField<String>();  
	    email.setFieldLabel("Email");  
	    fieldSet.add(email, data);  
	  
	    form2.add(fieldSet);  
	  
	    fieldSet = new FieldSet();  
	    fieldSet.setHeading("Phone Numbers");  
	    fieldSet.setCollapsible(true);  
	  
	    layout = new FormLayout();  
	    layout.setLabelWidth(75);  
	    fieldSet.setLayout(layout);  
	  
	    TextField<String> field = new TextField<String>();  
	    field.setFieldLabel("Home");  
	    fieldSet.add(field, data);  
	  
	    field = new TextField<String>();  
	    field.setFieldLabel("Business");  
	    fieldSet.add(field, data);  
	  
	    field = new TextField<String>();  
	    field.setFieldLabel("Mobile");  
	    fieldSet.add(field, data);  
	  
	    field = new TextField<String>();  
	    field.setFieldLabel("Fax");  
	    fieldSet.add(field, data);  
	  
	    form2.add(fieldSet);  
	    form2.setButtonAlign(HorizontalAlignment.CENTER);  
	    form2.addButton(new Button("Save"));  
	    form2.addButton(new Button("Cancel"));  
	  
	    vp.add(form2);  
	}
	
}
